package com.syni.mdd.sjb.common.datatreaure.listen.nacos;

import com.syni.mdd.sjb.common.component.utils.feign.service.general.MddCloudService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @className RegisterListener
 * @description TOO
 * @Author cfx
 * @DATE 2020/6/19 15:19
 * @VERSION 1.0
 **/
@Component
public class RegisterListener implements ApplicationListener<InstanceRegisteredEvent> {
    @Value("${jenkins-build-number:0}")
    private String version;

    @Value("${spring.application.name:未知应用}")
    private String applicationName;

    @Autowired
    MddCloudService mddCloudService;

    @Override
    public void onApplicationEvent(InstanceRegisteredEvent instanceRegisteredEvent) {
        try {
            if(!version.equals(Constants.COMMON_FLAG_FALSE)) {
                mddCloudService.updateCloudService(applicationName, version);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
